/*
LoveWaves Project - RFID reader test programme
Acceliis T4W RFID reader
Copyright (C) 2006, Eric Sigoillot (e.sigoillot@gmail.com)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
package lovewaves;

public class RFIDTest {

	/** T4W reader class */
	private T4Reader reader;
	/** Last tag selected */
	private TagInfo currentTag;
	
	/**
	* Constructor initializing reader
	*/
	public RFIDTest() throws T4ReaderException {
		/* Creates the reader */
		reader = new T4Reader();
		
		/* Registers ourselves as a tag selection listener */
		reader.addTagSelectedListener(new TagSelectedListener() {
			public void tagSelected(TagSelectedEvent e) {
				/* We print tag info only the first time a tag is presented */
				if (!e.getTagInfo().equals(currentTag)) {
					currentTag = e.getTagInfo();
					
					System.out.println(currentTag.toString() + "\n\n");
				}
			}
		});
	}

	/**
	* Runs the RFID test
	*/
	public void run() {
		/* Adds a program exit callback to terminate properly the T4 reader */
		Runtime.getRuntime().addShutdownHook(new Thread() {
			public void run() {
				try {
					reader.done();
				} catch (T4ReaderException e) {
					System.out.println("Error while terminating T4 reader!");
					System.out.println(e.getMessage() + "\n");
				}
				
				System.out.println("Terminating.\n");
			}
		});
		
		/* Initializes reader for tag selection */
		try {
			reader.init();
			System.out.println("RFID Reader ready...\n");
		} catch (T4ReaderException e) {
			System.out.println("Error while initializing reader!");
			System.out.println(e.getMessage() + "\n");
			System.exit(1);
		}
	}
	
	/**
	* Program entry point
	*/
	public static void main(String[] args) {
		/* Creates a test reader and runs the test */
		try {
			RFIDTest test = new RFIDTest();
			test.run();
		} catch (T4ReaderException e) {
			System.out.println("Cannot run test!");
			System.out.println(e.getMessage() + "\n");
		}
	}

}